package com.isunland.managesystem.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.ui.TableHeaderActivity;
import com.isunland.managesystem.ui.TableHeaderPagerActivity;
import com.isunland.managesystem.utils.LogUtil;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 带状态的列表封装类.
 *
 * @author wangyanan
 */
public class BaseTableListAdapter<T> extends ArrayAdapter<T> {

  private final T mObj;
  private LayoutInflater mInflater;
  private Context mContext;
  private Field[] colomns;//字段

  // Constructors

  /**
   * @param obj 新建一个对象传过来
   */
  public BaseTableListAdapter(Context context, List<T> objects,
      LinkedHashMap<String, String> titleFieldMap, T obj) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mContext = context;
    this.mObj = obj;
    if (titleFieldMap == null || titleFieldMap.size() == 0) return;

    ArrayList<String> titleList = new ArrayList<>();
    ArrayList<String> columnList = new ArrayList<>();
    for (String key : titleFieldMap.keySet()) {
      titleList.add(key);
      columnList.add(titleFieldMap.get(key));
    }

    setTitle(titleList.toArray(new String[titleFieldMap.size()]));
    setField(columnList.toArray(new String[titleFieldMap.size()]));
  }
  //public BaseTableListAdapter(Context context, T[] objects) {
  //    super(context, 0, objects);
  //    this.mInflater = LayoutInflater.from(context);
  //    this.mContext = context;
  //}

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_table, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }
    T item = getItem(position);
    bindData(position, vh, item);

    return vh.rootView;
  }

  public void bindData(int position, final ViewHolder vh, T item) {
    try {
      if (position % 2 == 0) {
        vh.rootView.setBackgroundColor(getContext().getResources().getColor(R.color.white));
      } else {
        vh.rootView.setBackgroundColor(
            getContext().getResources().getColor(R.color.standard_bg_split));
      }

      if (colomns == null || colomns.length == 0) return;

      for (int i = 0; i < colomns.length; i++) {
        vh.mColumnTvs[i].setVisibility(View.VISIBLE);
        Object colomnStr = colomns[i].get(item);
        if (colomnStr != null) {

          vh.mColumnTvs[i].setText(colomnStr.toString());
        } else {
          vh.mColumnTvs[i].setText("");
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void setTitle(String[] titles) {
    if (titles == null || titles.length == 0) return;
    if (mContext instanceof TableHeaderActivity) {

      ((TableHeaderActivity) mContext).setHeaderTitle(titles);
    }
    if (mContext instanceof TableHeaderPagerActivity) {

      ((TableHeaderPagerActivity) mContext).setHeaderTitle(titles);
    }
  }

  public void setField(String[] fields) {
    //((FixedHeaderActivity)mContext).setHeaderTitle(titles);

    if (fields == null || fields.length == 0) return;
    if (mObj == null) return;
    //       if (getCount()==0)return;
    //        T item = getItem(0);
    colomns = new Field[fields.length];
    try {
      for (int i = 0; i < fields.length; i++) {

        Field field = mObj.getClass().getDeclaredField(fields[i]);
        field.setAccessible(true);
        colomns[i] = field;
      }
    } catch (Exception e) {
      e.printStackTrace();
      LogUtil.e("getFieldError", e);
    }
  }

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-09-22 13:30:54 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  public static class ViewHolder {
    public final LinearLayout rootView;

    public final TextView[] mColumnTvs;

    private ViewHolder(LinearLayout rootView, TextView[] columnTvs) {
      this.rootView = rootView;

      this.mColumnTvs = columnTvs;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tv1 = (TextView) rootView.findViewById(R.id.tv_table_colomn_1);
      TextView tv2 = (TextView) rootView.findViewById(R.id.tv_table_colomn_2);
      TextView tv3 = (TextView) rootView.findViewById(R.id.tv_table_colomn_3);
      TextView tv4 = (TextView) rootView.findViewById(R.id.tv_table_colomn_4);
      TextView tv5 = (TextView) rootView.findViewById(R.id.tv_table_colomn_5);
      TextView tv6 = (TextView) rootView.findViewById(R.id.tv_table_colomn_6);
      TextView tv7 = (TextView) rootView.findViewById(R.id.tv_table_colomn_7);
      TextView tv8 = (TextView) rootView.findViewById(R.id.tv_table_colomn_8);
      TextView tv9 = (TextView) rootView.findViewById(R.id.tv_table_colomn_9);
      TextView tv10 = (TextView) rootView.findViewById(R.id.tv_table_colomn_10);
      TextView[] tvs = new TextView[] { tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8, tv9, tv10 };

      return new ViewHolder(rootView, tvs);
    }
  }
}